﻿/***************************************************************************************************
 * Filename:            SerializerHelper.cs
 * Module:              序列化模块
 * Copyright:           2008 穗联软件 版权所有
 * Author:              吴潮槟
 * Created Date:        2008-09-16
 * Last Modified Data:  
 * Description:         
***************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;

namespace GCELibrary.Current
{
    /// <summary>
    /// 序列化模块
    /// </summary>
    public class SerializerHelper
    {
        /// <summary>
        /// 将对象序列化为字符串
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>字符串结果</returns>
        public static string StringSerializer(object value)
        {
            string result = "";
            try
            {
                NetDataContractSerializer nSer = new NetDataContractSerializer();//实例化序列化对象
                MemoryStream stream = new MemoryStream();//实例内存流
                
                nSer.WriteObject(stream, value);//将对象写入流
                byte[] buf = stream.ToArray();//将流转换为字节数组
                result = Encoding.UTF8.GetString(buf, 0, buf.Length);//将字节数组转换为字符串
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 将字符串反序列化为对象
        /// </summary>
        /// <param name="str">序列化的字符串</param>
        /// <returns></returns>
        public static object StringDeserialize(string value)
        {
            object result = null;
            try
            {
                NetDataContractSerializer nSer = new NetDataContractSerializer();//实例反化序列化对象
                byte[] buf = Encoding.UTF8.GetBytes(value);//转换为字节数组
                MemoryStream stream = new MemoryStream(buf);//实例内存流
                stream.Seek(0, SeekOrigin.Begin);//把针指移到第一位
                result = nSer.ReadObject(stream);//反序列化
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 将对象序列化为流
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>流结果</returns>
        public static Stream StreamSerializer(object value)
        {
            Stream result =null;
            try
            {
                NetDataContractSerializer nSer = new NetDataContractSerializer();//实例化序列化对象
                MemoryStream stream = new MemoryStream();//实例内存流
                nSer.WriteObject(stream, value);//将对象写入流
                result = (Stream)stream;
                result.Seek(0, SeekOrigin.Begin);//把针指移到第一位
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 将流反序列化为对象
        /// </summary>
        /// <param name="str">序列化的流</param>
        /// <returns></returns>
        public static object StreamDeserialize(Stream value)
        {
            object result = null;

            if (!value.CanSeek)
            {
                value = GetNewStream(value);
            }
            try
            {
                NetDataContractSerializer nSer = new NetDataContractSerializer();//实例反化序列化对象
                value.Seek(0, SeekOrigin.Begin);//把针指移到第一位
                result = nSer.ReadObject(value);//反序列化
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 反序列化流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static T StreamDeserialize<T>(Stream stream)
        {
            object obj = StreamDeserialize(stream);
            if (obj is T)
            {
                return (T)obj;
            }
            else
            {
                return default(T);
            }
        }
        private static Stream GetNewStream(Stream value)
        {
            Stream resultStream = new MemoryStream();
            const int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = value.Read(buffer, 0, bufferLen)) > 0)
            {
                resultStream.Write(buffer, 0, count);
            }
            resultStream.Seek(0, SeekOrigin.Begin);//把针指移到第一位
            return resultStream;
        }
        /// <summary>
        ///  转换为json字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SerializerByJson(object value)
        {
            DataContractJsonSerializer serializer = new
    DataContractJsonSerializer(value.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, value);
            string jsonString = Encoding.Default.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
    }
}
